package com.yun.algorithmproblem.leetcode;

import java.util.HashMap;

public class Leetcode383 {
    //哈希表效率太慢，如果存储的全是符号，可以用一个固定长度数组代替哈希表
    public boolean canConstruct1(String ransomNote, String magazine) {
        HashMap<Character,Integer> map=new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            char c=magazine.charAt(i);
            if(!map.containsKey(c)){
                map.put(c,1);
            }else {
                map.put(c,map.get(c)+1);
            }
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            char c=ransomNote.charAt(i);
            if(!map.containsKey(c)){
                return false;
            }else {
                int count=map.get(c)-1;
                if(count<0){
                    return false;
                }else {
                    map.put(c,count);
                }
            }
        }
        return true;
    }

    public boolean canConstruct(String ransomNote, String magazine){
        int[] map=new int[26];
        for (int i = 0; i < magazine.length(); i++) {
            int idx=magazine.charAt(i)-'a';
            map[idx]++;
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            int idx=ransomNote.charAt(i)-'a';
            map[idx]--;
            if(map[idx]<0){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String ransomNote="abac";
        String magazine="b";
//        boolean res=new leetcode383().canConstruct(ransomNote,magazine);
//        System.out.println(res);
        System.out.println(ransomNote.toCharArray());
    }
}
